package com.css.dzj.zfjg.modules.supervisioncompinfo.service.impl;

import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisioncompinfo.mapper.JgFbComplaintMapper;
import com.css.dzj.zfjg.modules.supervisioncompinfo.mapper.JgTsjbComplaintInfoMapper;
import com.css.dzj.zfjg.modules.supervisioncompinfo.mapper.JgTsjbMediateInfoMapper;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.QJgTsjbComplaintInfoEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.QJgFbComplaintEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.QJgTsjbUserInfoEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.JgTsMsg;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class JgFbComplaintServiceImpl implements JgFbComplaintService {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private JgFbComplaintMapper jgFbComplaintMapper;
    @Autowired
    private JgTsjbComplaintInfoMapper jgTsjbComplaintInfoMapper;
    @Autowired
    JgTsjbMediateInfoMapper jgTsjbMediateInfoMapper;
    /**
     * 分页查询监管投诉列表
     *
     */
    @Override
    public QueryResults<JgTsMsg> getTsComplainEntity(Pageable pageable, String complaintTerritory, String complaintObject, String fbBureau,String fbDepartment,
                                           String complaintType, String name, String taskState, String receiveStartTime,
                                           String receiveEndTime) {
        //监管投诉信息表
        QJgTsjbComplaintInfoEntity qEntity  = QJgTsjbComplaintInfoEntity.jgTsjbComplaintInfoEntity;
        //投诉举报用户信息表
        QJgTsjbUserInfoEntity dEntity = QJgTsjbUserInfoEntity.jgTsjbUserInfoEntity;
        //投诉任务分办表
        QJgFbComplaintEntity bEntity = QJgFbComplaintEntity.jgFbComplaintEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ("".equals(complaintTerritory) || complaintTerritory == null) ? predicate : ExpressionUtils.and(predicate, qEntity.complaintTerritory.like("%"+complaintTerritory+"%"));
        predicate = ("".equals(complaintObject) || complaintObject == null) ? predicate : ExpressionUtils.and(predicate, qEntity.complaintObject.like("%"+complaintObject+"%"));
        predicate = ("".equals(complaintType) || complaintType == null) ? predicate : ExpressionUtils.and(predicate, qEntity.complaintType.like("%"+complaintType+"%"));
        predicate = ("".equals(taskState) || taskState == null) ? predicate : ExpressionUtils.and(predicate, qEntity.taskState.eq(taskState));
        //predicate = ("".equals(status) || status == null) ? predicate : ExpressionUtils.and(predicate, qEntity.status.like("%"+status+"%"));
        predicate = ("".equals(name) || name == null) ? predicate : ExpressionUtils.and(predicate, dEntity.name.like("%"+name+"%"));
        predicate = ("".equals(fbBureau) || fbBureau == null) ? predicate : ExpressionUtils.and(predicate, bEntity.fbBureau.like("%"+fbBureau+"%"));
        predicate = ("".equals(fbDepartment) || fbDepartment == null) ? predicate : ExpressionUtils.and(predicate, bEntity.fbDepartment.like("%"+fbDepartment+"%"));
        //任务接受时间
        if((StringUtils.isEmpty(receiveStartTime)) && (!StringUtils.isEmpty(receiveEndTime))) {
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                endDate = sf.parse(receiveEndTime + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.loe(endDate));
        }
        if ((!StringUtils.isEmpty(receiveStartTime)) && (StringUtils.isEmpty(receiveEndTime))) {
            Date startDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(receiveStartTime+" "+"00:00:00.000");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.goe(startDate));
        }
        if ((!StringUtils.isEmpty(receiveStartTime)) && (!StringUtils.isEmpty(receiveEndTime))) {
            Date startDate = null;
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(receiveStartTime+" "+"00:00:00.000");
                endDate = sf.parse(receiveEndTime + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.between(startDate, endDate));
        }

        return jpaQueryFactory
                .select(
                        Projections.bean(JgTsMsg.class,
                                qEntity.registerCode,qEntity.complaintTerritory,qEntity.complaintObject,bEntity.fbBureau,bEntity.fbDepartment,qEntity.complaintType,dEntity.name,qEntity.receiveTime,qEntity.taskState))
                .from(qEntity)
                .leftJoin(dEntity).on(qEntity.registerCode.eq(dEntity.registerCode))
                .leftJoin(bEntity).on(qEntity.registerCode.eq(bEntity.registerCode))
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    /**
     * 通过registercode查询监管投诉信息
     */
    @Override
    public JgTsjbComplaintInfoEntity findNo(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbComplaintInfoEntity qEntity  = QJgTsjbComplaintInfoEntity.jgTsjbComplaintInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }

    /**
     * 通过registercode查询监管任务分办表
     */
    @Override
    public JgFbComplaintEntity findRegister(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgFbComplaintEntity qEntity  = QJgFbComplaintEntity.jgFbComplaintEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }

    /**
     * 通过registercode查询任务催办表
     */
    @Override
    public JgTsjbUrgeInfoEntity findCode(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbUrgeInfoEntity qEntity  = QJgTsjbUrgeInfoEntity.jgTsjbUrgeInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }
    /**
     * 通过registercode查询调解信息
     */
    @Override
    public JgTsjbMediateInfoEntity findMediate(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbMediateInfoEntity qEntity  = QJgTsjbMediateInfoEntity.jgTsjbMediateInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }

    /**
     * 通过registercode查询任务经办表
     */
    @Override
    public JgTsjbHandleInfoEntity fidByRegister(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbHandleInfoEntity qEntity  = QJgTsjbHandleInfoEntity.jgTsjbHandleInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }
    /**
     * 通过registercode查询投诉人员信息
     */
    @Override
    public JgTsjbUserInfoEntity findUser(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbUserInfoEntity qEntity  = QJgTsjbUserInfoEntity.jgTsjbUserInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }
    /**
     * 添加监管投诉分办信息
     * @param jgFbComplaintEntity
     */
    @Transactional
    @Override
    public void add(JgFbComplaintEntity jgFbComplaintEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbComplaintEntity.setCreateUser(userInfo.getLoginName());
            }
            jgFbComplaintEntity.setFbState("101");
            jgFbComplaintEntity.setCreateTime(new Date());
            //查收时间（认领时间）
            jgFbComplaintEntity.setReceiveTime(new Date());
            jgFbComplaintMapper.save(jgFbComplaintEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 修改监管分办信息
     * @param jgFbComplaintEntity
     */
    @Transactional
    @Override
    public void update(JgFbComplaintEntity jgFbComplaintEntity, HttpServletRequest request) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbComplaintEntity.setUpdateUser(userInfo.getLoginName());
            }
            jgFbComplaintEntity.setUpdateTime(new Date());
            jgFbComplaintMapper.save(jgFbComplaintEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 修改监管投诉信息
     * @param jgTsjbComplaintInfoEntity
     */
    @Transactional
    @Override
    public void update(JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity, HttpServletRequest request) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgTsjbComplaintInfoEntity.setUpdateUser(userInfo.getLoginName());
            }
            if(StringUtils.isEmpty(jgTsjbComplaintInfoEntity.getFbTime())){
                jgTsjbComplaintInfoEntity.setFbTime(new Date());
            }
            jgTsjbComplaintInfoEntity.setUpdateTime(new Date());
            jgTsjbComplaintInfoEntity.setTaskState("101");
            jgTsjbComplaintInfoMapper.save(jgTsjbComplaintInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 添加调解信息
     * @param jgTsjbMediateInfoEntity
     */
    @Transactional
    @Override
    public void addmedite(JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity, HttpServletRequest request) {
        try {
            jgTsjbMediateInfoMapper.save(jgTsjbMediateInfoEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
